home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 234_01 / secure.c < prev    next >
Text File  |  1987-06-16  |  3KB  |  150 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     XDIR - Hard Disk Manager
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Apr 03, 1987
  6.   DESCRIPTION:     Hard Disk Manager for IBM PC
  7.   KEYWORDS:     Hard Disk Manager Dump Directory
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      secure.c
  10.   WARNINGS:     None
  11.   CRC:         N/A
  12.   SEE-ALSO:     HDIR.DOC and XDIR.DOC
  13.   AUTHOR:     Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
  14.   COMPILERS:     ECO-C
  15.   REFERENCES:     XDIR.DOC
  16. */
  17.  
  18.  
  19. /*
  20.     secure.c
  21.     from DDJ 5/86
  22.  
  23. */
  24. #include "stdio.h"
  25. #define BUFSIZE 16384
  26. #define NEWBUF 2000
  27. #define NUMPRIMES 50
  28.  
  29. /*
  30.    secure
  31.    security encryption module
  32.  
  33. */
  34. int secure(ifile,ofile,key,keycount)
  35. char *ifile;      /* file to encrypt */
  36. char *ofile;      /* name of encrypted output file */
  37. char *key;        /* key value for encryption */
  38. int  keycount;    /* number of keys */
  39. {
  40.     int  fdin, fdout;
  41.     int  n, count;
  42.     char *inbuf;
  43.     int  argc;
  44.     char *calloc(int, int);
  45.     static char *argv[3] = {
  46.     NULL,NULL,"                      "};
  47.     static char keyx[20];
  48.  
  49.     strcpy(argv[2],key);
  50.     strcpy(keyx,key);
  51.     argc = 3 + keycount;
  52.     inbuf = calloc(BUFSIZE,1);
  53.     if  (inbuf == NULL) return(-1);
  54.  
  55.  
  56.     if ((fdin = open(ifile,0)) == EOF)
  57.        {
  58.        writestr("\nCan't open input ");
  59.        writestr(ifile);
  60.        getch();
  61.        return(-1);
  62.        }
  63.  
  64.     if ((fdout = creat(ofile,0)) == EOF)
  65.        {
  66.        writestr("\nCan't open output ");
  67.        writestr(ofile);
  68.        getch();
  69.        return(-1);
  70.        }
  71.  
  72.     while (1)
  73.        {
  74.        count = read(fdin,inbuf,BUFSIZE);
  75.        n = 3;
  76.  
  77.        while (n++ <argc)
  78.           {
  79.           /* keyx = argv[n-1]; */
  80.           cypher(inbuf,count,keyx);
  81.           }
  82.  
  83.        n = write(fdout,inbuf,count);
  84.        if (count < BUFSIZE) break;
  85.        }
  86.  
  87.  
  88.        close(fdin);
  89.        close(fdout);
  90.        return (0);
  91.  
  92. }
  93.  
  94. /*
  95.    cypher1.c
  96.    DDJ 5/86
  97.  
  98. */
  99.  
  100. void cypher(buffer,num,code)
  101. char *buffer, *code;
  102. int num;
  103. {
  104. static int i, n, index, length;
  105. static int sum, keylength;
  106. static char *newkey;
  107. static int prime[] = {
  108.       1009, 1999, 1013, 1997, 1019,
  109.       1993, 1021, 1987, 1031, 1979,
  110.       1033, 1973, 1039, 1951, 1049,
  111.       1949, 1051, 1933, 1061, 1931,
  112.       1063, 1913, 1069, 1907, 1087,
  113.       1901, 1091, 1889, 1093, 1879,
  114.       1097, 1877, 1103, 1873, 1109,
  115.       1871, 1117, 1867, 1123, 1861,
  116.       1129, 1847, 1151, 1831, 1153,
  117.       1823, 1163, 1813, 1171, 1803
  118.       };
  119.  
  120.  
  121.    char *calloc(int, int);
  122.    newkey = calloc(NEWBUF,1);  /* allocate for new key */
  123.    keylength = sum = 0;
  124.    while (n = (int) code[keylength])  /* key length and sum check for each key*/
  125.      {
  126.      sum += n;
  127.      ++keylength;
  128.      }
  129.  
  130.     length = prime[sum % NUMPRIMES];
  131.  
  132.     for (i=0;i<length;i++)
  133.     {
  134.     index = i % keylength;
  135.     sum = code[index] + sum & 0xff;
  136.     newkey[i] = code[index] ^ sum;
  137.     }
  138.  
  139.     /* encrypt the file with the generated key */
  140.  
  141.  
  142.     for (i=0;i<=num;i++)
  143.        buffer[i] = buffer[i] ^ newkey[i % length];
  144.  
  145.     free(newkey);     /* release storage */
  146.  
  147. }   /* end cypher1.c */
  148.  
  149.  
  150.